<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">小方店铺</div></nav-bar>
    <tab-control
      :title="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      class="tab-control"
      v-show="isTabFixed"
    />
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
      :pull-up-load="true"
      @pullingUp="loadMore"
    >
      <home-swiper :banner="banner" @swiperImageLoad="swiperImageLoad" />
      <recommend-views :recommend="recommend" />
      <FeatrueView />
      <tab-control
        :title="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      />
      <goods-list :goods="showGoods"/>
    </scroll>
    <back-top @click.native="backClick" v-show="backIsShow" />
  </div>
</template>

<script>
// 公共组件
import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
// 子组件
import HomeSwiper from "./childComps/HomeSwiper";
import RecommendViews from "./childComps/RecommendViews";
import FeatrueView from "./childComps/FeatrueView";
// 网络请求文件
import { getHomeRecommend, getGoods } from "network/home";
import { debounce } from "common/utils";

import { itemImageListener, isShowBackTop } from "common/mixin";

export default {
  name: "Home",
  data() {
    return {
      banner: [],
      recommend: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      current: "pop",
      taboffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  mixins: [itemImageListener, isShowBackTop],
  computed: {
    showGoods() {
      return this.goods[this.current].list;
    },
  },
  components: {
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    HomeSwiper,
    RecommendViews,
    FeatrueView,
  },
  destroyed() {
    // console.log('home被销毁')
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
    this.$refs.scroll.refresh();
  },
  deactivated() {
    // 1.保存Y值
    this.saveY = this.$refs.scroll.getScrollY();
    // 2.取消全局事件的监听
    this.$bus.$off("itemImgLoad", this.itemImageListener);
  },

  created() {
    // 请求多个数据
    this.getHomeRecommend();
    // 请求商品数据
    this.getGoods("pop");
    this.getGoods("new");
    this.getGoods("sell");
  },
  mounted() {
    // 1.图片加载完成的事件监听
    // 监听item中图片加载完成
  },
  methods: {
    /*
      事件监听相关的方法
      */
    tabClick(index) {
      switch (index) {
        case 0:
          this.current = "pop";
          break;
        case 1:
          this.current = "new";
          break;
        case 2:
          this.current = "sell";
          break;
      }
      this.$refs.tabControl1.current = index;
      this.$refs.tabControl2.current = index;
    },
    // backClick() {
    //   this.$refs.scroll.scrollTo(0, 0)
    // },
    contentScroll(position) {
      // 1.判断backtop是否显示
      this.ShowBackTop(position);
      // 2.决定tabcontrol是否吸顶（position：fixed）
      this.isTabFixed = -position.y > this.taboffsetTop;
    },
    loadMore() {
      this.getGoods(this.current);
    },
    swiperImageLoad() {
      // 2.获取tabControl的offsetTop
      this.taboffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    /*
      发送网络请求的方法
      */
    getHomeRecommend() {
      getHomeRecommend().then((res) => {
        this.banner = res.data.banner.list;
        this.recommend = res.data.recommend.list;
      });
    },
    getGoods(type) {
      const page = this.goods[type].page + 1;
      getGoods(type, page).then((res) => {
        // console.log(res.data.list);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        // 完成上拉加载更多
        this.$refs.scroll.finishPullUp();
      });
    },
  },
};
</script>

<style scoped>
#home {
  height: 100vh;
  /* padding-top: 44px; */
  position: relative;
}

.home-nav {
  width: 100%;
  color: #ffffff;
  background-color: var(--color-tint);
}

.content {
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden;
  /*下面这个方法，如果用鼠标滚轮滚过会导致多出一片空白*/
  /*height: calc(100% - 93px);*/
  /*overflow: hidden;*/
  /*margin-top: 44px;*/
}
.tab-control {
  position: relative;
  z-index: 9;
}

</style>
